$(document).ready(function(){
     // Validate patient's birthday
    $('input#DOB_yyyy').blur(function() {
        checkBirthday($(this),$('#DOB_mm'),$('#DOB_dd'));
    });
    $('input#DOB_mm').blur(function(){
        checkMonth($(this),false,'','statusMessage');
    });   
    $('input#DOB_dd').blur(function(){
        checkDay($(this),false,'','statusMessage');
    });
    $('input#personAgeTxt').change(function() {       
    	checkAge('DOB_yyyy', 'DOB_mm', 'DOB_dd', 'personAgeTxt', null, 'statusMessage');
    });
    // Province changed
    $('select#provinceLbx').change( function() {
        provinceChange('provinceLbx', 'districtLbx');       
    });
    // Set autocomplete for ward input
    setAutoComplete2("wardNameTxt", "../Patient/getWardFromDistrictAjax?set_ajax_view&part=","district_id","districtLbx");
    
    addDatePickerTime("#checkIn_dd",'#checkIn_mm','#checkIn_yyyy','#checkInTimeTxt',false,false,true);
    
});

function checkBirthday(objYear, objMonth, objDay) {
        var res     =   checkDate(objYear, objMonth, objDay, false, _('Empty date of birth'), 'statusMessage', true);
        if(!res){
            return false;
        }
        $('input#personAgeTxt').css("border-color", "#AAA");
        getAge('DOB_yyyy','personAgeTxt');
        return true;    
}



function saveDeadOutsideInfo(print) {
    if(!isReadyToSubmit()){
        $('span#msgSavingStatus').css('color', 'red').text(_('Fail! Please check the required fields')).show();
        setTimeout("clearText('msgSavingStatus')", 10000);        
        return;
    }
    else{
        $('span#msgSavingStatus').css('color', 'green').text('');
        showLoader(true);
    }
    var submitData = $('#emerReceptionFrm').formSerialize();
    $.ajax ({
        type:    "POST",
        url:     "../EmerReception/SaveReportDeadOutsideInfo?set_ajax_view",
        data:    submitData,
        dataType: "json",
        beforeSend: function() {            
           
        },
        complete: function() {
            
        },
        success: function(data) {
            $('span#msgSavingStatus').css('color', 'green').text(_('Save successful')).show();
            showLoader(false);
            $('#deathInfoIdHdn').val(data.deathInfoId);
            setTimeout("clearText('msgSavingStatus')", 10000);
            if (print) {
                printReportDeadOusite(data.deathOutsideInfoId);
            }
        }
    });
}

function isReadyToSubmit() {
	if (!checkAge('DOB_yyyy', 'DOB_mm', 'DOB_dd', 'personAgeTxt', null, 'statusMessage')) {
		return false;
	}
	
    if (!checkBirthday($('input#DOB_yyyy'),$('input#DOB_mm'), $('input#DOB_dd'))) {
        return false;
    }     
    if(!checkTimeCheckIn()){
        return false;
    }
    if(!checkReceptionDoctor()){
        return false;
    }
    $('span#statusMessage').text('');
    return true;
}

function checkTimeCheckIn(){
    var res =   checkFullDate($('#checkIn_yyyy'), $('#checkIn_mm'), $('#checkIn_dd'), true,  _('Empty check-in date'), 'statusMessage', true);
    if(!res){
        return false;
    }
    res = checkHour($('#checkInTimeTxt'), true, _('Invalid time'), 'statusMessage');
    if(!res){
        return false;
    }
    return true;
}
function checkReceptionDoctor(){
    if ( $('#doctorLbx option:selected').val() == 0 ) {
        $('#doctorLbx').css("border-color", "red");
        $('span#statusMessage').text(_('Select reception doctor')).show();
        return false;
    }
    else {
        $('#doctorLbx').css("border-color", "#AAA");  
        return true;
    }
}

function showLoader(show){
    if(show){
       $('#actionButtonDiv').hide(); 
       $('#loaderDiv').show();
    }  else {
        $('#actionButtonDiv').show(); 
        $('#loaderDiv').hide();
    }
}

function clearText(id){
    $('#' + id).text('');
}

function printReportDeadOusite(deathInfoId){
    var url='../EmerPrint/PrintReportDeadOutside?set_print_view&deathInfoId=' + deathInfoId;
    window.open(url, '', 'scrollbars=yes, menubar=no, width=800, height=600, resizable=yes, toolbar=no, location=no, status=no');
}

function provinceChange(provinceId, districtId) {
    $.ajax ({
        type:    "GET",
        url:      "../Patient/getDistrictFromProvinceAjax?set_ajax_view",
        data:     "provice_id=" + $('#' + provinceId).val(),
        dataType: "json",
        success:    function(data) {
            if(data.districtList.length > 0){
                $('#' + districtId).html(data.districtList);
            }else{
                var option  =   "<option value='0' label='- " + _('District') + " -'>- "  + _('District') + " -</option>";
                $('#' + districtId).html(option);
            }
        }
    });
}

function autoTab(thisElem, maxLength, nextElem){
	if(thisElem.value.length == maxLength){
		thisElem.blur();
		$('#' + nextElem).focus();
	}
}